
package com.v1.video.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iss.view.common.ToastAlone;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.R;
import com.v1.video.domain.VersionCheckPageInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.exception.ServicesException;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;
import com.v1.video.widgets.MyDialog;

/**
 * 
 * update lixinli 2.14/6/19
 *      增加页面打开统计
 *
 */
public class AboutActivity extends BaseActivity {

    private final String TAG = "AboutActivity";

    private AboutActivity instance;

    /** 新手引导 */
    RelativeLayout layout_freshman_guide = null;

    /** 用户使用协议 */
    RelativeLayout layout_users_protocol = null;

    /** 意见反馈 */
    RelativeLayout layout_idea_feedback = null;

    /** 关于我们 */
    RelativeLayout layout_about = null;

    /** 检测版本 */
    RelativeLayout layout_check_version = null;
    
//    /** 评分 */
//    RelativeLayout layout_grade = null;
    
    /** 应用推荐 */
    RelativeLayout layout_app_commend = null;

    /** 标题 */
    TextView text_title;

    /** 返回按钮 */
    private ImageView view_btn_return;
    private TextView txtVer = null;
    
    private boolean isCheck = false;
    private String version = "";
    private int ver1 = 0;
    private String PackageName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        instance = this;        
    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
        layout_freshman_guide = (RelativeLayout) findViewById(R.id.about_freshman_guide_bg);
        layout_users_protocol = (RelativeLayout) findViewById(R.id.about_user_protocol_bg);
        layout_idea_feedback = (RelativeLayout) findViewById(R.id.about_idea_feedback_bg);
        layout_about = (RelativeLayout) findViewById(R.id.about_about_bg);
        layout_check_version = (RelativeLayout) findViewById(R.id.about_check_version_bg);
//        layout_grade = (RelativeLayout) findViewById(R.id.about_grade_bg);
        layout_app_commend = (RelativeLayout)findViewById(R.id.about_app_commend);
        view_btn_return = (ImageView) findViewById(R.id.iv_result);
        text_title = (TextView) findViewById(R.id.tv_title);
        
        txtVer = (TextView)findViewById(R.id.about_version_now_content);
    }

    @Override
    protected void initData() {
        // TODO Auto-generated method stub
        text_title.setText(R.string.about_title_about);
        PackageManager pm = getPackageManager();//context为当前Activity上下文 
        PackageInfo pi;
        try {
            PackageName = getPackageName();
            pi = pm.getPackageInfo(PackageName, 0);
            version = pi.versionName;
            ver1 = pi.versionCode;
        } catch (NameNotFoundException e) {
        }
        txtVer.setText("Ver"+version);
        Logger.i(TAG, "txtVer="+version);
    }

    @Override
    protected void setListener() {
        // TODO Auto-generated method stub
        layout_freshman_guide.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//                Intent intent = new Intent();
//                intent.setClass(AboutActivity.this, AboutFreshManActivity.class);
//                startActivity(intent);
            }
        });
        layout_users_protocol.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(AboutActivity.this, AboutUserProtocolActivity.class);
                startActivity(intent);
            }
        });
        layout_idea_feedback.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(AboutActivity.this, AboutIdeaActivity.class);
                startActivity(intent);
            }
        });
        layout_about.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(AboutActivity.this, AboutDetailActivity.class);
                startActivity(intent);
            }
        });
        layout_check_version.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(!Helper.checkConnection(AboutActivity.this)){
                    ToastAlone.showToast(AboutActivity.this, getResources().getString(R.string.net_no), 1);
                    return;
                }
                if(!isCheck){
                    isCheck = true;
                    new GetVersionAsync().execute();
                }
            }
        });       
        
        findViewById(R.id.lay_result).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
//        view_btn_return.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // TODO Auto-generated method stub
//                finish();
//            }
//        });
        layout_app_commend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent_app = new Intent(AboutActivity.this, SettingRecommendAppActivity.class);
                startActivity(intent_app);
			}
		});
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }
    private MyDialog dialogCommon;
    /**更新版本的提示*/
    private void updataShow(String msg,final String url,String ver) {
        if(dialogCommon != null){
            dialogCommon = null;
        }
        dialogCommon = new MyDialog(this, R.style.dialog_custom);
        RelativeLayout linearlayout;
        android.view.WindowManager.LayoutParams layoutparams;
        linearlayout = (RelativeLayout)((LayoutInflater)this.getSystemService("layout_inflater")).inflate(R.layout.dialog_version_updata, null);
        linearlayout.setMinimumWidth(10000);
        
//        layoutparams = dialogCommon.getWindow().getAttributes();
//        layoutparams.x = 0;
//        layoutparams.y = -1000;
//        layoutparams.gravity = 80;
//        dialogCommon.onWindowAttributesChanged(layoutparams);
        
        dialogCommon.setCanceledOnTouchOutside(true);
        dialogCommon.setContentView(linearlayout);
        dialogCommon.setCancelable(false);
        dialogCommon.setCanceledOnTouchOutside(false);
        //
//        dialogCommon.setContentView(R.layout.dialog_person_mobile);
//        dialogCommon.setCancelable(false);
//        dialogCommon.setCanceledOnTouchOutside(false);
        if(!msg.equals("")){
            String strMess = msg.trim();
            strMess = strMess.replaceAll("。", "\n");
            ((TextView)dialogCommon.findViewById(R.id.version_intro_1)).setText(strMess);
        }else{
            ((TextView)dialogCommon.findViewById(R.id.version_intro_1)).setText(getResources().getString(R.string.version_updata_1)+"Ver"+ver);
        }        
        dialogCommon.findViewById(R.id.version_btn_cancel).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialogCommon.dismiss();
            }
        });  
        dialogCommon.findViewById(R.id.version_btn_sure).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialogCommon.dismiss();
                Uri uri = Uri.parse(url);
//                String urll = "http://t.cn/8knIGJb";
//                Uri uri = Uri.parse(urll); 
                Intent it   = new Intent(Intent.ACTION_VIEW,uri); 
                startActivity(it);
            }
        });
        dialogCommon.show();
    }

    /**
     * 检测版本
     */
    private class GetVersionAsync extends AsyncTask<Void, Void, Boolean> {
        
        private String errorMsg = "";
        
        private ProgressDialog pd;
        VersionCheckPageInfo resultInfo = null;
        
        public GetVersionAsync() {                        
           
        }
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = Utils.getProgressDialog(AboutActivity.this,getResources().getString(R.string.get_data), this);
            pd.show();
        }
        
        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                NetEngine engine = new NetEngine();
                resultInfo = engine.getVersionData();
            } catch (ServicesException e) {
                e.printStackTrace();
                errorMsg = e.getMessage();
            }
            return false;
            
        }
//        Uri uri = Uri.parse(versionInfo.url); 
//        Intent it   = new Intent(Intent.ACTION_VIEW,uri); 
//        startActivity(it);
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if(pd != null && pd.isShowing()){
                pd.cancel();
            }
            isCheck = false;
            if (errorMsg.equals("")) {
                if(resultInfo != null){
//                    ToastAlone.showToast(AboutActivity.this, resultInfo.getResult().getMessage(), 1);
//                    aboutIdeaResult();
                    
                    float ver2 = resultInfo.getVersion_number();
                    Logger.i(TAG, "resultInfo.getVersions()=="+resultInfo.getVersions());
                    if(ver1 < ver2){
                        updataShow(resultInfo.getDetails(),resultInfo.getDownload_url(),resultInfo.getVersion_name());
                    }else{
                        ToastAlone.showToast(AboutActivity.this, getResources().getString(R.string.version_noupdata), 1);
                    }
                }else{
                    ToastAlone.showToast(AboutActivity.this, resultInfo.getResult().getMessage(), 1);
                }
            } else {
                ToastAlone.showToast(AboutActivity.this, errorMsg, 1);
            }
        }
    }
    
    /**
     * 测试注册
     */
    private class GetDataAsync extends AsyncTask<Void, Void, Boolean> {
        
        private String errorMsg = "";
        
        private ProgressDialog pd;
        VersionCheckPageInfo resultInfo = null;
        
        public GetDataAsync() {                        
           
        }
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = Utils.getProgressDialog(AboutActivity.this,getResources().getString(R.string.get_data), this);
            pd.show();
        }
        
        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                NetEngine engine = new NetEngine();
                engine.getTestRegistData();
            } catch (Exception e) {
                e.printStackTrace();
                errorMsg = e.getMessage();
            }
            return false;
            
        }
//        Uri uri = Uri.parse(versionInfo.url); 
//        Intent it   = new Intent(Intent.ACTION_VIEW,uri); 
//        startActivity(it);
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if(pd != null && pd.isShowing()){
                pd.cancel();
            }
            isCheck = false;
            if (errorMsg.equals("")) {
                if(resultInfo != null){
//                    ToastAlone.showToast(AboutActivity.this, resultInfo.getResult().getMessage(), 1);
//                    aboutIdeaResult();
                    float ver1 = Float.parseFloat(version);
                    float ver2 = Float.parseFloat(resultInfo.getVersions());
                    Logger.i(TAG, "resultInfo.getVersions()=="+resultInfo.getVersions());
                    if(ver1 < ver2){
                        updataShow(resultInfo.getDetails(),resultInfo.getDownload_url(),resultInfo.getVersions());
                    }else{
                        ToastAlone.showToast(AboutActivity.this, getResources().getString(R.string.version_noupdata), 1);
                    }
                }else{
                    ToastAlone.showToast(AboutActivity.this, resultInfo.getResult().getMessage(), 1);
                }
            } else {
                ToastAlone.showToast(AboutActivity.this, errorMsg, 1);
            }
        }
    }
    
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("关于第一视频页");
        MobclickAgent.onResume(this);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("关于第一视频页");
        MobclickAgent.onPause(this);
    }
}
